<!DOCTYPE html>
<head>
<script src="../resources/gc.js"></script>
<script src="../resources/js-test.js"></script>
<script src="resources/netinfo_common.js"></script>
</head>
<body>
<script>

description('Tests that listeners in closed frames are collected.');

shouldBe('typeof internals.observeGC', '"function"',
'this test requires window.internals');

var childFrame;
var childFrameObserver, childConnectionObserver, callbackObserver;

// Do initialization work in an inner function to avoid references to
// objects remaining live on this function's stack frame.
// (http://crbug.com/595672/).
(function() {
    var callback = function(e) {
	      testFailed("Should not get here.");
    };

    childFrame = document.createElement('iframe');
    childFrameObserver = internals.observeGC(childFrame);
    document.body.appendChild(childFrame);
    var childConnection = childFrame.contentWindow.navigator.connection;
    childConnectionObserver = internals.observeGC(childConnection);
    callbackObserver = internals.observeGC(callback);
    childConnection.addEventListener('typechange', callback);
})();

asyncGC().then(() => {
  shouldBeFalse('childFrameObserver.wasCollected');
  shouldBeFalse('childConnectionObserver.wasCollected');
  shouldBeFalse('callbackObserver.wasCollected');

  document.body.removeChild(childFrame);
  childFrame = null;

  return asyncGC();
}).then(() => {
  shouldBeTrue('childFrameObserver.wasCollected');
  shouldBeTrue('childConnectionObserver.wasCollected');
  shouldBeTrue('callbackObserver.wasCollected');

  finishJSTest();
});

</script>
</body>
</html>
